
import Throttling from '../../utils/throttling';

function computedWatchInner (tableRef) {
  this.tableWatch(tableRef);
}

export default option => {
  let {
    data,
  } = option;
  let dataStep = {};
  let computedStep = {};
  let watchStep = {};
  function tablePublic (object, tableRef) {
    let {column, key, tableColumn, ref} = object;
    let tableColumnStep = [];
    if (tableColumn && !isNaN(tableColumn)) {
      for (let i = 0; i < tableColumn; i++) {
        tableColumnStep.push(column[i]);
      }
      delete object.tableColumn;
    }
    return {
      page: 1,
      pageSizes: [10, 20, 50, 100],
      rows: 10,
      total: 0,
      layout: 'total, sizes, prev, pager, next, jumper',
      small: false,
      muti: true,
      data: [],
      ref: ref || tableRef,
      multiSelect: false,
      multipleSelection: [],
      key: key || column[0].prop,
      tableColumn: tableColumnStep.length ? tableColumnStep : [column[0], column[1]],
      tableColumnAll: column,
    };
  }
  function tableItem ({column}, tableRef) {
    computedStep['autoVue_' + tableRef] = null;
    let comp = {};
    for (let item of column) {
      let {search} = item;
      if (search) {
        dataStep['autoVueTable_' + item.prop] = '';
        watchStep['autoVueTable_' + item.prop] = function () {
          let _this = this;
          Throttling(computedWatchInner, {
            throttlingTime: 500,
            context: _this,
            data: tableRef,
          });
        };
        comp[item.prop] = '';
      }
    }
    computedStep['autoVue_' + tableRef] = function () {
      for (let index in comp) {
        if (comp.hasOwnProperty(index)) {
          comp[index] = this['autoVueTable_' + index];
        }
      }
      return comp;
    };
  }
  for (let tableRef in data) {
    if (data.hasOwnProperty(tableRef) && /^table/.test(tableRef)) {
      if (typeof data[tableRef].column === 'undefined') {
        data[tableRef] = {column: data[tableRef]};
      }
      tableItem(data[tableRef], tableRef);
      data[tableRef] = {
        ...tablePublic(data[tableRef]),
        ...data[tableRef],
      };
      data[tableRef].ref = tableRef;
      delete data[tableRef].column;
    }
  }
  option.data = {
    ...option.data,
    ...dataStep,
  };
  option.computed = {
    ...option.computed,
    ...computedStep,
  };
  option.watch = {
    ...option.watch,
    ...watchStep,
  };
  option.methods['tableWatch'] = function (tableRef) {
    let {
      page,
      rows,
    } = this[tableRef];
    let step = {};
    for (let index in this['autoVue_' + tableRef]) {
      if (this['autoVue_' + tableRef].hasOwnProperty(index)) {
        step[index + '_cons'] = this['autoVue_' + tableRef][index];
      }
    }
    this.post(this[tableRef].url, {
      ...step,
      page,
      rows,
    })
      .then(response => {
        if (this[tableRef].cb) {
          this[tableRef].cb.call(this, {
            data: response.contentList,
            total: response.totals,
          });
        } else {
          this[tableRef].data = response.contentList;
          this[tableRef].total = response.totals;
        }
      });
  };
  option.methods['tableIds'] = function (tableRef) {
    let ids = [];
    for (let item of this[tableRef].multipleSelection) {
      ids.push(item.id);
    }
    if (typeof this[tableRef].ids !== 'undefined') {
      return [
        ...ids,
        ...this[tableRef].ids,
      ];
    } else {
      return ids;
    }
  };
  option.methods['tableClear'] = function (tableRef) {
    this[tableRef].page = 1;
    this[tableRef].rows = 10;
    this[tableRef].total = 0;
    this[tableRef].data = [];
    if (typeof this.$refs[tableRef] !== 'undefined') {
      this.$refs[tableRef].$refs[tableRef].clearSelection();
    }
  };
};
